using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.AspNetCore.Http;

namespace Core.SimpleTemp.Api.ZB
{
    /// <summary>
    /// ZB_SafetyMilestone
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class ZB_SafetyMilestoneController : CoreApiController
    {
        private readonly CoreDBContext _coreDBContext;
        private readonly ZB_SafetyMilestoneService _service;
        /// <summary>
        /// ZB_SafetyMilestone
        ///</summary>
        public ZB_SafetyMilestoneController(ZB_SafetyMilestoneService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(ZB_SafetyMilestoneController) + "_" + nameof(ZB_SafetyMilestoneController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<ZB_SafetyMilestoneDto>
                pagingQueryModel)
        {
            IPageModel<ZB_SafetyMilestoneDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(ZB_SafetyMilestoneController) + "_" + nameof(ZB_SafetyMilestoneController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(ZB_SafetyMilestoneDto dto)
        {
            dto.CreateFactory = UserIdentity.Factory;

            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                dto.activityTime = dto.activityTime.Value.AddHours(8);
                dto.year = dto.activityTime.Value.Year;
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                dto.activityTime = dto.activityTime.Value.AddHours(8);
                if (dto.year != dto.activityTime.Value.Year)
                {
                    dto.year = dto.activityTime.Value.Year;
                }
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(ZB_SafetyMilestoneController) + "_" + nameof(ZB_SafetyMilestoneController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {
            List<Guid> newList = new List<Guid>();
            for (int i = 0; i < ids.Count(); i++)
            {
                if (_coreDBContext.ZB_SafetyMilestone.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
                {
                    newList.Add(ids[i]);
                }
            }
            await _service.DeleteBatchAsync(newList.ToArray());
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            //if(model.activityTime.Value.Year < model.year)
            //{
            //    model.activityTime = model.activityTime.Value.AddHours(8);
            //}
          
            var retModel = new
            {
                model.Id,
                model.activityName,
                model.activityTime,
                model.year,
                model.activtyTimeinterval,
                model.prizeResearchProcess,
                model.main,
                //model.Dateofaddition,
                //model.Conferencecontent,
                //model.AttachmentId,
                //model.AttachmentName,
            };
            //  附件
            //var attachmentList = await _coreDBContext.SysFile.Where(a => a.B_ID == id).Select(a => "/api/File/DownLoad?id=" + a.Id).ToListAsync();

            return this.JsonSuccess(retModel);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(ZB_SafetyMilestoneController) + "_" + nameof(ZB_SafetyMilestoneController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<ZB_SafetyMilestoneDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("Import")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + "导入")]
        public async Task<IActionResult> ImportAsync([FromForm]String data, [FromForm]List<IFormFile> file)
        {
            var retMeg = await _service.ImportAsync(file[0], data);
            if (!retMeg.Any())
            {
                return this.JsonSuccess();
            }
            else
            {
                return this.JsonFaild(retMeg[0].ToString());
            }
        }
        #endregion


    }
}
